/**********************************************************************
 Copyright (C) 2009 Jaakko Vuori

 This file is part of Cimer.
 For more information, see <http://code.google.com/p/cimer/>

 Cimer is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 Cimer is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with Cimer.  If not, see <http://www.gnu.org/licenses/>.
 **********************************************************************/

#include "cimerwindow.h"
#include "ui_cimerwindow.h"

#include <QSettings>
#include <QDebug>
#include <QSound>
#include <QSignalMapper>
#include "screensaverpreventer.h"

CimerWindow::CimerWindow(QWidget *parent) :
    QMainWindow(parent, Qt::WindowStaysOnTopHint),
    ui(new Ui::CimerWindow),
    m_screensaverPreventer(NULL),
    m_soundEnabled(false),m_lockEnabled(false)
{
    ui->setupUi(this);

    connect(ui->pieWidget, SIGNAL(timeElapsed()), this, SLOT(handletimeElapsed()));
    connect(ui->pieWidget, SIGNAL(timeAdjusted(int)), this, SLOT(handletimeAdjusted(int)));

    QSettings settings("Cimer", "Cimer");
    restoreGeometry(settings.value("geometry").toByteArray());
    restoreState(settings.value("windowState").toByteArray());

#ifndef NO_SOUND
    m_soundEnabled = settings.value("soundEnabled", m_soundEnabled).toBool();
#endif



    createMenu();

    ui->pieWidget->setFocus();
}

CimerWindow::~CimerWindow()
{
    delete m_screensaverPreventer;
    delete ui;
}

void CimerWindow::closeEvent(QCloseEvent* event)
{
    QSettings settings("Cimer", "Cimer");
    settings.setValue("geometry", saveGeometry());
    settings.setValue("windowState", saveState());
#ifndef NO_SOUND
    settings.setValue("soundEnabled", m_soundEnabled);
#endif


    QMainWindow::closeEvent(event);
}

void CimerWindow::setTime(int minutes)
{
    if(!m_lockEnabled)
    ui->pieWidget->setTime(QTime(minutes / 60, minutes % 60));

}

void CimerWindow::handletimeAdjusted(int secondsLeft)
{
    qDebug() << "CimerWindow::handletimeAdjusted" << secondsLeft;
    if (secondsLeft > 0)
    {
        if (!m_screensaverPreventer)
        {
            qDebug() << "Start screensaver prevention";
            m_screensaverPreventer = new ScreensaverPreventer;
        }
    }
    else
    {
        qDebug() << "Stop screensaver prevention due to adjust to 0";
        delete m_screensaverPreventer;
        m_screensaverPreventer = NULL;
    }
}

void CimerWindow::handletimeElapsed()
{
    qDebug() << "Stop screensaver prevention due to time elapsed";
    delete m_screensaverPreventer;
    m_screensaverPreventer = NULL;
    playAudioNotification();
}

void CimerWindow::playAudioNotification()
{
    if (!m_soundEnabled)
    {
        qDebug() << "CimerWindow::playAudioNotification (with sound disabled)";
        return;
    }

    qDebug() << "CimerWindow::playAudioNotification";

    // Play audio clip for timeout notification
    QString timeoutFile("timeout.wav");

#ifdef Q_OS_SYMBIAN
    // The audio files will be stored in application's private folder in Symbian
    QString privateFolder = QCoreApplication::applicationDirPath() + "/";
    timeoutFile = QString(privateFolder + timeoutFile);
#endif // Q_OS_SYMBIAN

    QSound::play(timeoutFile);
    qDebug() << "CimerWindow::playAudioNotification - " + timeoutFile;
}

void CimerWindow::enableSound(bool enabled)
{
    m_soundEnabled = enabled;
}
void CimerWindow::enableLock(bool enabled)
{
    m_lockEnabled = enabled;

}


#ifdef Q_OS_SYMBIAN
void CimerWindow::createMenu()
{
    QList<int> defaults;
    defaults << 0 << 15 << 30 << 60;

    QSignalMapper* mapper = new QSignalMapper(this);
    connect(mapper, SIGNAL(mapped(int)), this, SLOT(setTime(int)));
    foreach(int minutes, defaults)
    {
        QAction* minuteAction = new QAction(QString("%1 min").arg(minutes), this);
        connect(minuteAction, SIGNAL(triggered()), mapper, SLOT(map()));
        mapper->setMapping(minuteAction, minutes);

        ui->menubar->addAction(minuteAction);
    }

    QMenu* settingsMenu = ui->menubar->addMenu(tr("Settings"));

    #ifndef NO_SOUND
    QAction* soundEnabledAction = new QAction(tr("Sound"), this);
    soundEnabledAction->setCheckable(true);
    soundEnabledAction->setChecked(m_soundEnabled);
    settingsMenu->addAction(soundEnabledAction);
    connect(soundEnabledAction, SIGNAL(toggled(bool)), this, SLOT(enableSound(bool)));
    #endif

    #ifndef NO_LOCK
    QAction* lockEnabledAction = new QAction(tr("Lock"), this);
    lockEnabledAction->setCheckable(true);
    lockEnabledAction->setChecked(m_lockEnabled);
    settingsMenu->addAction(lockEnabledAction);
    connect(lockEnabledAction, SIGNAL(toggled(bool)), this, SLOT(enableLock(bool)));
    #endif

}
#else // For other operating systems than Symbian

void CimerWindow::createMenu()
{
    QMenu* optionsMenu = ui->menubar->addMenu(tr("Options"));

    #ifndef NO_SOUND
    QAction* soundEnabledAction = new QAction(tr("Sound"), this);
    soundEnabledAction->setCheckable(true);
    soundEnabledAction->setChecked(m_soundEnabled);
    optionsMenu->addAction(soundEnabledAction);
    connect(soundEnabledAction, SIGNAL(toggled(bool)), this, SLOT(enableSound(bool)));
    #endif

    #ifndef NO_LOCK
    QAction* lockEnabledAction = new QAction(tr("Lock"), this);
    lockEnabledAction->setCheckable(true);
    lockEnabledAction->setChecked(m_lockEnabled);
    optionsMenu->addAction(lockEnabledAction);
    connect(lockEnabledAction, SIGNAL(toggled(bool)), this, SLOT(enableLock(bool)));
    #endif

    QAction* separator = new QAction(this);
    separator->setSeparator(this);
    optionsMenu->addAction(separator);

    QList<int> defaults;
    defaults << 0 << 15 << 30 << 60;

    QSignalMapper* mapper = new QSignalMapper(this);
    connect(mapper, SIGNAL(mapped(int)), this, SLOT(setTime(int)));
    foreach(int minutes, defaults)
    {
        QAction* minuteAction = new QAction(QString("%1 min").arg(minutes), this);
        connect(minuteAction, SIGNAL(triggered()), mapper, SLOT(map()));
        mapper->setMapping(minuteAction, minutes);

        optionsMenu->addAction(minuteAction);
    }

}


#endif
